﻿using System.Xml;
using System.Collections.ObjectModel;

namespace Builderator.Core
{
    public class List : ObservableCollection<string>
    {
        public List()
            : base()
        {

        }

        public List(string settings)
            : this()
        {
            if (settings == string.Empty)
                return;

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(settings);

            XmlElement rootElement = xmlDoc.DocumentElement;

            foreach (XmlElement element in rootElement.ChildNodes)
            {
                this.Add(element.InnerText);
            }
        }

        public override string ToString()
        {
            XmlDocument xmlDoc = new XmlDocument();

            XmlElement childElement;
            XmlElement rootElement = xmlDoc.CreateElement("List");

            foreach (string item in this)
            {
                childElement = xmlDoc.CreateElement("ListItem");

                childElement.InnerText = item;

                rootElement.AppendChild(childElement);
            }

            xmlDoc.AppendChild(rootElement);

            return xmlDoc.InnerXml;
        }
    }
}